﻿using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Terraria;
using Terraria.GameContent.UI.Elements;
using Terraria.ID;
using Terraria.Localization;
using Terraria.UI;

namespace HardcoreGame.Common.UI
{
    public class AgeDisplayUI : UIState
    {
        private UIText _ageText;
        private UIText _nextAgeText;
        private const float XPosition = -620f;

        public override void OnInitialize()
        {
            _ageText = new UIText("", 0.85f);
            _ageText.Left.Set(XPosition, 1f);
            _ageText.Top.Set(50f, 0f);
            Append(_ageText);

            _nextAgeText = new UIText("", 0.7f);
            _nextAgeText.Left.Set(XPosition, 1f);
            _nextAgeText.Top.Set(75f, 0f);
            Append(_nextAgeText);
        }

        public void UpdateDisplay(int currentAge, int daysToNextAge)
        {
            _ageText.SetText(Language.GetTextValue("Mods.HardcoreGame.UI.16") + $"{currentAge}" + Language.GetTextValue("Mods.HardcoreGame.UI.15"));

            if (daysToNextAge > 0)
            {
                _nextAgeText.SetText(Language.GetTextValue("Mods.HardcoreGame.UI.17") + $"{daysToNextAge}"+ Language.GetTextValue("Mods.HardcoreGame.UI.10"));
                _nextAgeText.TextColor = daysToNextAge <= 10 ?
                    Color.Lerp(Color.Gold, Color.Orange, Main.rand.NextFloat()) :
                    Color.LightGray;
            }
            else
            {
                _nextAgeText.SetText(Language.GetTextValue("Mods.HardcoreGame.UI.18"));
                _nextAgeText.TextColor = Color.Lerp(Color.Gold, Color.Yellow, (float)Main.time % 1f);
            }
            if (daysToNextAge == 0 && Main.time % 30 < 15)
            {
                Dust.NewDustPerfect(
                    new Vector2(Main.screenWidth + XPosition + 100, 85),
                    DustID.Confetti,
                    Vector2.UnitY * -2,
                    100, Color.White, 1.2f
                );
            }
        }

        public override void Draw(SpriteBatch spriteBatch)
        {
            if (!Main.player[Main.myPlayer].dead && !Main.gameMenu)
            {
                base.Draw(spriteBatch);
            }
        }
    }
}
